package com.ikkong.system.util;

import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import org.beetl.sql.core.kit.StringKit;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.*;


public class CommonUtil {
	public static Map mapwhere(String where) throws UnsupportedEncodingException {
		List<String> strings = new ArrayList<>();
		if(StringKit.isNotBlank(where)){
			where = URLDecoder.decode(where,"utf-8");
			where =  where.replaceAll("\"","");
			where =  where.replaceAll("_2nd","");
			where =  where.replaceAll("_equal","");
			where =  where.replaceAll("or_","");
			where =  where.replaceAll("\'","");
			where = where.replaceAll("\\{","");
			where = where.replaceAll("\\}","");
			where = where.replaceAll(",",":");
			strings =  Arrays.asList(where.split(":"));
		}
		Map mt = new HashMap();
		if(strings.size()== 2){
			mt.put(strings.get(0),"%"+strings.get(1)+"%");
		}else if(strings.size()==4){
			mt.put(strings.get(0),"%"+strings.get(1)+"%");
			mt.put(strings.get(2),"%"+strings.get(3)+"%");
		}else if(strings.size()==6){
			mt.put(strings.get(0),"%"+strings.get(1)+"%");
			mt.put(strings.get(2),"%"+strings.get(3)+"%");
			mt.put(strings.get(4),"%"+strings.get(5)+"%");
		}else if(strings.size()==8){
			mt.put(strings.get(0),"%"+strings.get(1)+"%");
			mt.put(strings.get(2),"%"+strings.get(3)+"%");
			mt.put(strings.get(4),"%"+strings.get(5)+"%");
			mt.put(strings.get(6),"%"+strings.get(7)+"%");
		}
		return mt;
	}
	public static Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			return null;
		}
	}
	public static Map<?, ?> objectToMap(Object obj) {
		if (obj == null) {
			return null;
		}
		return new org.apache.commons.beanutils.BeanMap(obj);
	}

	public static Object fenye(List maps,int page,int rows){
		int start = (page-1)*rows;
		double d = maps.size()*1.00/rows*1.00;
		int end = start + rows;
		if (end>maps.size()){
			end = maps.size();
		}
		List<Map> newmap = maps.subList(start,end);
		Map sss = new HashMap();
		sss.put("rows",newmap);
		sss.put("total",Math.ceil(d));
		sss.put("page",page);
		sss.put("records",maps.size());
		Object o = sss;
		return o;
	}

	public static Object fenye(List maps,int page,int rows,int size){
		int start = (page-1)*rows;

		double d = size/rows;
		int end = start + rows;
		if (end>size){
			end = size;
		}
		Map sss = new HashMap();
		if(start >= size){
			sss.put("rows",new ArrayList());
		}else {
			sss.put("rows",maps);
		}

		sss.put("total",Math.ceil(d));
		sss.put("page",page);
		sss.put("records",size);


		Object o = sss;
		return o;
	}


	public static void createFieldNameList(List<Map<String, Object>> fieldNameList, String index, String width, String textAlign, String onRenderEvent, boolean isHidden) {
		Map<String, Object> fieldNameMap = new HashMap<String, Object>();
		fieldNameMap.put("index", index);
		fieldNameMap.put("width", width);
		fieldNameMap.put("textAlign", textAlign);
		fieldNameMap.put("isHidden", isHidden);
		fieldNameMap.put("onRenderEvent", onRenderEvent);
		fieldNameMap.put("isSort", true);
		fieldNameList.add(fieldNameMap);
	}
	public static boolean ynadmin(){
        List<String> roleList = ShiroKit.getUser().getRoleList();
        boolean b = false;
        for(String ss:roleList){
            if("1".equals(ss)){
                b = true;
                break;
            }
        }
        return b;
    }
	public static boolean ynsuper(){
		List<String> roleList = ShiroKit.getUser().getRoleList();
		boolean b = false;
		for(String ss:roleList){
			if("1".equals(ss)){
				b = true;
				break;
			}
		}
		return b;
	}
	public static String quchong(String s){

		String[] array = s.split(",");
		Set<String> set = new HashSet<>();
		for(int i=0;i<array.length;i++){
			set.add(array[i]);
		}
		String[] arrayResult = (String[]) set.toArray(new String[set.size()]);
		return Arrays.toString(arrayResult);
	}
}
